<?php  

if( ! defined('BASEPATH')) exit('No direct script access allowed');

class customer extends CI_Controller{
	function __construct(){
		parent::__construct();
		
		// check if customer logged in 
		if(!$this->ion_auth->logged_in()){
			redirect('auth/login');
		}		
		$this->load->library('form_validation');
		$this->load->model('people/customer_model');		
		$this->load->library('ion_auth');
		$this->load->library('session');
		
		$this->load->library('form_validation');
		$this->load->helper('url');

		// Load MongoDB library instead of native db driver if required
		$this->config->item('use_mongodb', 'ion_auth') ?
		$this->load->library('mongo_db') :

		$this->load->database();
		$this->load->library('datatables');	

		$this->form_validation->set_error_delimiters($this->config->item('error_start_delimiter', 'ion_auth'), $this->config->item('error_end_delimiter', 'ion_auth'));
	


	}
	
	function get(){
		echo $this->customer_model->get();
	}
		
	function create_customer($flag=NULL)
	{
		
		$this->data['page_title'] = "Create Customer";
		$meta['page_title'] = "Create Customer";
		
	//	$this->data['groups']=$this->accgroup_model->get_group();
		/*if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin())
		{
			redirect('auth', 'refresh');
		}for checking the authentication*/

		//validate form input
		$this->form_validation->set_rules('name', 'Customer Name', 'required');
		$this->form_validation->set_rules('contact_person', 'contact_person', '');
		$this->form_validation->set_rules('opening_balance', 'opening_balance', '');
		$this->form_validation->set_rules('address', 'address', '');
		$this->form_validation->set_rules('tin_no', 'tin_no', '');
		$this->form_validation->set_rules('phone', 'phone no', '');
		$this->form_validation->set_rules('email', 'email', '');
		$this->form_validation->set_rules('mobile', 'mobile no', '');
		if ($this->form_validation->run() == true)
		{
			$data = array(
				'name' =>strtoupper( $this->input->post('name')),
				'contact_person' =>strtoupper( $this->input->post('contact_person')),
				'phone' =>strtoupper( $this->input->post('phone')),
				'mobile' =>strtoupper( $this->input->post('mobile')),
				'tin_no' =>strtoupper( $this->input->post('tin_no')),
				'email' =>strtoupper( $this->input->post('email')),
				'address'  => strtoupper($this->input->post('address')),
				'type'  =>  $this->input->post('type'),
				'opening_balance' =>$this->input->post('opening_balance')
			);
			
			$account_data= array(
								'opening_balance' => $this->input->post('opening_balance'),
								'name' => strtoupper( $this->input->post('name')),
								'type' =>  $this->input->post('type')
								);
			//print_r($additional_data);exit;
		}
		if ($this->form_validation->run() == true) 
		{
				//echo "i am here";exit;
				$id_customer = $this->customer_model->insert_customer($data);
				$account_data['id_user'] =$id_customer;
				$this->customer_model->insert_account($account_data);
			//echo $flag;exit;
			if(isset($flag)){
				redirect("sales/sales/add_sales");
			}else{
				redirect("people/customer/get_all_customer");
			
			}
			//	print_r("i am here");exit;
				redirect("people/customer/get_all_customer");
			}
		else
		{
			//display the create user form
			//set the flash data error message if there is one
			$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));

			$this->data['name'] = array(
				'name'  => 'name',
				'id'    => 'name',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('name'),
			);
			
			$this->data['contact_person'] = array(
				'name'  => 'contact_person',
				'id'    => 'contact_person',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('contact_person'),
			);

			
			$this->data['email'] = array(
				'name'  => 'email',
				'id'    => 'email',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('email'),
			);
			
			$this->data['phone'] = array(
				'name'  => 'phone',
				'id'    => 'phone',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('phone'),
			);
			
			$this->data['mobile'] = array(
				'name'  => 'mobile',
				'id'    => 'mobile',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('mobile'),
			);
			$this->data['tin_no'] = array(
				'name'  => 'tin_no',
				'id'    => 'tin_no',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('tin_no'),
			);
			$this->data['opening_balance'] = array(
				'name'  => 'opening_balance',
				'id'    => 'opening_balance',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('opening_balance'),
			);
			$this->data['type'] = array(
				'name'  => 'type',
				'id'    => 'type',
				'type'  => 'select',
				'value' => $this->form_validation->set_value('type'),
				
			);
	
					
			$this->data['address'] = array(
				'name'  => 'address',
				'id'    => 'address',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('address'),
			);
		//	print_r($this->data);exit;
			if(isset($flag)){
				$this->data['flag'] = 'st';
			}
			$this->load->view('common/header',$meta);
			$this->load->view('people/customer/create_customer', $this->data);
			$this->load->view('common/footer');
		}
	}	
	
	
	
	function get_all_customer(){
		//$this->data['users'] = $this->customer_model->get_customerList()->result();
		$meta['page_title'] = "customer List";
		$this->data['message'] = "customer Added Successfully";
		$this->load->view('common/header', $meta);
		$this->load->view('people/customer/view_customer1');
		$this->load->view('common/footer');
	   
	}
	

	function edit_customer($id){
	
		$this->data['page_title'] = "Edit customer";
		$meta['page_title'] = "Edit customer";

		if(!$this->ion_auth->logged_in()){
			redirect('auth', 'refresh');
		}
		$this->data['branches']=$this->customer_model->get_branch();
		$user = $this->customer_model->edit_customer($id)->row();
		$this->form_validation->set_rules('name', 'Customer Name', 'required|xss_clean');
		//$this->form_validation->set_rules('tin_no', 'Tin Number', 'required|xss_clean');
		//$this->form_validation->set_rules('last_name', 'Last Name', 'required|xss_clean');
		//$this->form_validation->set_rules('phone_no', 'Phone Number', 'required|xss_clean|min_length[10]|is_natural_no_zero|max_length[10]');
		/*$this->form_validation->set_rules('phone2', 'Second Part of Phone', 'required|xss_clean|min_length[3]|max_length[3]');
		$this->form_validation->set_rules('phone3', 'Third Part of Phone', 'required|xss_clean|min_length[4]|max_length[4]');
		$this->form_validation->set_rules('company', 'Company Name', 'required|xss_clean');*/
		
		//$this->form_validation->set_rules('groups', 'Groups', 'xss_clean');

		if(isset($_POST) && !empty($_POST)){
			// do we have a valid request?
			/*if ($this->_valid_csrf_nonce() === FALSE || $id != $this->input->post('id'))
			{
			show_error('This form post did not pass our security checks.');
			}*/
			$data = array(
				'name'  => strtoupper($this->input->post('name')),
				'contact_person'  => strtoupper($this->input->post('contact_person')),
				'tin_no'      => strtoupper($this->input->post('tin_no')),
				'phone'      => $this->input->post('phone'),
				'mobile'      => $this->input->post('mobile'),
				'email' =>	strtoupper($this->input->post('email')),
				'opening_balance' =>	strtoupper($this->input->post('opening_balance')),
				'type' =>   $this->input->post('type'),
				'address' =>	strtoupper($this->input->post('address'))
			);
					
						$account_data= array(
								'opening_balance' => $this->input->post('opening_balance'),
								'name' => strtoupper( $this->input->post('name')),
								'type' =>  $this->input->post('type')
								);
			
			
			if($this->form_validation->run() === TRUE){
				
				$this->customer_model->update($user->id, $data);
				$account_data['id_user'] = $user->id;	
				$this->customer_model->update_account($account_data);
				$this->session->set_flashdata('message', "customer Saved");
				redirect("people/customer/get_all_customer");
			}
		}
		$this->data['message'] = (validation_errors() ? validation_errors() : ($this->ion_auth->errors() ? $this->ion_auth->errors() : $this->session->flashdata('message')));
		$this->data['name'] = array(
			'name'  => 'name',
			'id'    => 'name',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('name', $user->name),
		);
	$this->data['contact_person'] = array(
			'name'  => 'contact_person',
			'id'    => 'contact_person',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('contact_person', $user->contact_person),
		);
	
		$this->data['tin_no'] = array(
			'name'  => 'tin_no',
			'id'    => 'tin_no',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('tin_no',$user->tin_no),
		);
		$this->data['phone'] = array(
			'name'  => 'phone',
			'id'    => 'phone',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('phone', $user->phone),
		);
		$this->data['mobile'] = array(
			'name'  => 'mobile',
			'id'    => 'mobile',
			'type'  => 'text',
			'value' => $this->form_validation->set_value('mobile', $user->mobile),
		);
		$this->data['email'] = array(
			'name' => 'email',
			'id'   => 'email',
			'type' => 'text',
			'value' => $this->form_validation->set_value('email', $user->email),
		);
		$this->data['address'] = array(
			'name' => 'address',
			'id'   => 'address',
			'type' => 'textarea',
			'value' => $this->form_validation->set_value('address', $user->address),
		);
			$this->data['opening_balance'] = array(
				'name'  => 'opening_balance',
				'id'    => 'opening_balance',
				'type'  => 'text',
				'value' => $this->form_validation->set_value('opening_balance',$user->opening_balance),
			);
		
		$this->data['submit'] = array(
			'name' => 'submit',
			'id'   => 'submit',
			'type' => 'submit',
			'value' =>'Save Edit',
			'class' =>'btn btn-success'
		);
		$this->data['user'] = $user;
		$meta['page_title'] = "Edit customer";
		$this->load->view('common/header',$meta);
		$this->load->view('people/customer/edit_customer', $this->data);
		$this->load->view('common/footer');
	}

	function delete_customer($id){
		
		$this->customer_model->delete($id);
		redirect("people/customer/get_all_customer");
	
	}
	
	function checkgroup($str){
		if($str==0){
			$this->form_validation->set_message('checkgroup', 'The %s field can not be Empty! ');
			return FALSE;
		}else{
			return TRUE;
		}
				
	}
		
	
	function check_customer(){
		if($this->customer_model->check_customer($this->input->post('name'),$this->input->post('group'))){
			$this->form_validation->set_message('check_customer', 'customer Exists With The same group');
			return FALSE;
		}else{
			return TRUE;
		}
	
	}
	
}
	

?>